package com.systemCapacity.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.systemCapacity.common.dto.BattleCapacityDto;
import com.systemCapacity.common.dto.CapacityRelatedTypeRequestDto;
import com.systemCapacity.common.dto.CapacityRelatedTypesSaveDto;
import com.systemCapacity.common.dto.EquipmentTemplateDto;
import com.systemCapacity.common.lang.Result;
import com.systemCapacity.entity.BattleCapacity;
import com.systemCapacity.service.BattleCapacityService;
import com.systemCapacity.service.BattleTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 任杰
 * @since 2023.6.15
 */
@RestController
@RequestMapping("/capacity")
public class BattleCapacityController {
    @Autowired
    BattleCapacityService battleCapacityService;

    @Autowired
    BattleTaskService battleTaskService;

    /**
     *
     * @param type 能力所属十大能力
     * @param firstName 搜索能力名称
     * @return 返回十大能力下能力树
     */
    @GetMapping("/list")
    public Result getCapacities(@RequestParam(value = "type", defaultValue = "0") int type,
                                @RequestParam(value = "firstName", required = false) String firstName) {
        return Result.succ(battleCapacityService.getBattleCapacities(type, firstName));
    }

    /**
     * 添加作战能力
     * @param dto 作战能力dto
     * @return 新增作战能力结果
     */
    @PostMapping("/save")
    public Result addCapacity(@RequestBody BattleCapacityDto dto) {
        Assert.notNull(dto.getCapacityType(), "请选择能力所属十大能力");
        if (battleCapacityService.capacityNameExist(dto.getCapacityName())) {
            if (dto.getPid() != null && !dto.getPid().isEmpty()) {
                return Result.fail("二级能力名称重复，请重新输入");
            } else {
                return Result.fail("一级能力名称重复，请重新输入");
            }
        }
        battleCapacityService.saveBattleCapacity(dto);
        return Result.succ(null);
    }

    /**
     * 删除作战能力
     */
    @GetMapping("/delete")
    public Result deleteCapacity(String capacityId) {
        battleCapacityService.removeById(capacityId);
        return Result.succ("删除成功");
    }

    /**
     * 编辑作战能力
     * @param dto 作战能力dto
     * @return 编辑作战能力结果
     */
    @PostMapping("/edit")
    public Result editCapacity(@RequestBody BattleCapacityDto dto) {
        Assert.notNull(dto.getCapacityId(), "能力为空，请选择作战能力进行编辑");
        if (!battleCapacityService.capacityExist(dto.getCapacityId())) {
            return Result.fail("当前作战能力不存在");
        }
        if (dto.getPid() == null || dto.getPid().isEmpty()) {
            Assert.isNull(dto.getRelatedType(), "一级作战能力不能关联作战装备");
            QueryWrapper<BattleCapacity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("capacityName", dto.getCapacityName());
            queryWrapper.eq("pid", "");
            BattleCapacity battleCapacity = battleCapacityService.getOne(queryWrapper);
            if (battleCapacity != null && !battleCapacity.getCapacityId().equals(dto.getCapacityId())) {
                return Result.fail("一级能力名称重复，请重新输入");
            }
        } else {
            if (!battleCapacityService.capacityExist(dto.getPid())) {
                return Result.fail("当前作战能力父能力不存在");
            }
            QueryWrapper<BattleCapacity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("pid", dto.getPid());
            queryWrapper.eq("capacityName", dto.getCapacityName());
            BattleCapacity battleCapacity = battleCapacityService.getOne(queryWrapper);
            if (battleCapacity != null && !battleCapacity.getCapacityId().equals(dto.getCapacityId())) {
                return Result.fail("二级能力名称重复，请重新输入");
            }
        }
        battleCapacityService.editBattleCapacity(dto);
        return Result.succ(null);
    }

    /**
     * 查看十大能力下一级能力
     * @param type 十大能力类型
     * @param firstName 一级能力名称
     * @return 查询结果
     */
    @GetMapping("/first/list")
    public Result getFirstCapacities(@RequestParam(value = "type", defaultValue = "0") int type,
                                     @RequestParam(value = "firstName", required = false) String firstName) {
        return Result.succ(battleCapacityService.getFirstCapacities(type, firstName));
    }

    /**
     * 查看能力相关作战任务
     * @param capacityId 能力id
     * @return 相关作战任务列表
     */
    @GetMapping("/plans")
    public Result getRelatedBattleActivity(@RequestParam(value = "capacityId") String capacityId) {
        if (!battleCapacityService.capacityExist(capacityId)) {
            return Result.fail("作战任务不存在");
        }
        return Result.succ(battleTaskService.getRelatedBattleTask(capacityId));
    }


    @PostMapping("/type/save")
    public Result saveRelatedTypes(@RequestBody CapacityRelatedTypeRequestDto dto) {
        return Result.succ(battleCapacityService.saveRelatedTypes(dto.getDtos()));
    }

    /**
     *获取表头
     */
    @GetMapping("/columnList")
    public Result getTableColumnList() {
        List<EquipmentTemplateDto> list = battleCapacityService.getTableColumnList();
        List<String> res = list.stream().map(EquipmentTemplateDto::getTemplateName).collect(Collectors.toList());
        return Result.succ(res);
    }
}
